package org.phpgwt.phpgenerator;

import java.lang.reflect.Method;
import java.util.ArrayList;

import org.phpgwt.Utility;
import org.phpgwt.annotations.GenerateStubs;

public class PHPStubsGenerator {
	public static String generate(String className) throws ClassNotFoundException {
		Class<?> clazz = Class.forName(className);

		if (clazz.isAnnotationPresent(GenerateStubs.class)) {
			ArrayList<String> php = new ArrayList<String>();

			php.add("<?php");
			php.add("");

			for (Method method : clazz.getMethods()) {
				String signature = String.format("function %s(", method.getName());
				for (int i = 0; i < method.getParameterTypes().length; i++) {
					signature += String.format("$%s%s", method.getParameterTypes()[i].getSimpleName().toLowerCase() + i,
							i < method.getParameterTypes().length - 1 ? ", " : "");
				}
				signature += "){";
				php.add(signature);
				php.add("}");
				php.add("");
			}

			php.add("");
			php.add("?>");

			return Utility.arrayListToString(php);
		} else {
			return null;
		}
	}
}
